<template>
  <div class="home">
    <img alt="Vue logo" src="../assets/logo.png">
    <h2>扫码登录 案例</h2>
    <p>Nodejs + Mysql + Vue + Uni-app</p>
    <div v-if="ScanCodeShow">
      <div class="stateQRcode" >
        <div class="successQRcode" v-if="successShow">
          <img src="@/assets/img/success.png" alt="">
          <p>扫码成功</p>
        </div>
        <img :src="base64QRcode.QRcode" alt="">
      </div>
      <div class="stateText">
        <p>{{tipsText}}</p>
      </div>
    </div>
    <div v-else>
      登录成功
    </div>
  </div>
</template>

<script>
// 引入socket.io
import socket from '@/assets/js/socket.io.min.js'

export default {
  name: 'Home',
  data () {
    return {
      socket: null, // 保存 socket链接
      base64QRcode: '', // 保存后端给的二维码和uuid
      timerCheck: null, // 保存定时器
      tipsText: '请扫码', // 提示文字
      successShow: false, // 是否显示扫码成功
      ScanCodeShow: true // 是否显示二维码登录部分内容
    }
  },
  created () {
    this.socket = socket.io('ws://localhost:3000') // 默认连接了localhost
    this.getQRcode()
    this.ScanCodeing()
    this.ScanCodeOvertime()
  },
  methods: {
    getQRcode () {
      // 发起获取登录二维码请求
      this.socket.emit('getQRcode')
      this.sendQRcode()
    },
    sendQRcode () {
      // 获取二维码数据
      this.socket.on('sendQRcode', res => {
        // console.log(res)
        this.base64QRcode = res
      })
      this.checkScanCode()
    },
    checkScanCode () {
      // 等待扫码
      this.socket.on('waitScanCode', res => {
        if (!this.successShow) {
          this.tipsText = '等待用户扫码'
        }
      })
      // 扫码成功
      this.socket.on('SuccessScanCode', res => {
        localStorage.setItem('userInfo', res)
        this.tipsText = `扫码成功用户信息为：${res}`
        // this.ScanCodeShow = false
        this.socket.disconnect()
        clearInterval(this.timerCheck)
      })
      // 轮询，每隔1秒向服务器查看是否扫码登录成功
      this.timerCheck = setInterval(() => { this.socket.emit('checkScanCode', this.base64QRcode.uuid) }, 1000)
    },
    ScanCodeing () {
      // 已扫码但未确认
      this.socket.on('ScanCodeing', (res) => {
        if (res !== this.base64QRcode.uuid) {
          return false
        }
        this.successShow = true
        this.tipsText = '您已扫码，请在手机上确认登录！'
        this.socket.emit('deteleTimer', this.base64QRcode.uuid)
      })
    },
    ScanCodeOvertime () {
      // 如果二维码超时
      this.socket.on('ScanCodeOvertime', (value) => {
        if (value === this.base64QRcode.uuid) {
          this.successShow = false
          this.getQRcode()
        }
        // console.log('二维码已经重新获取')
      })
    }
  }
}
</script>

<style lang='less' scoped>
.home{
  display:flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  flex-wrap: wrap;
  text-align: center;
  .stateQRcode{
    position: relative;
    margin: 20px;
    .successQRcode{
      position:absolute;
      width: 100%;
      height: 100%;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      background: rgba(0, 0, 0, 0.5);
      color:white;
      img{
        width: 50px;
        height: 50px;
      }
    }
  }
  .stateText{
    border-radius: 25px;
    background: rgba(0, 0, 0, 0.5);
    color:white;
    padding: 0 20px;
  }
}
</style>
